<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');
require_once('../shared/confirmation.php');

class Phone extends Layout {

    protected $objAlert, $dlgConfirmation, $mctContact, $dtgPhone;
    protected $txtNumber;
    protected $btnSave, $btnCancel;
    protected $pxyNew;
    protected $intId;

    protected function Form_PreRender() {
        parent::Form_PreRender();

        $this->dtgPhone->Refresh();
    }

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/contact/menu/phone.tpl.php';
        
        $this->objAlert = new Alert($this);

        $this->dlgConfirmation = new Confirmation($this, null, new QAjaxAction('dlgConfirmation_Confirm'), null);

        $this->pxyNew = new QControlProxy($this);
        $this->pxyNew->AddAction(new QClickEvent(), new QAjaxAction('btnNew_Click'));
        $this->pxyNew->AddAction(new QClickEvent(), new QTerminateAction());

        $this->mctContact = ContactMetaControl::CreateFromQueryString($this, QMetaControlCreateType::EditOnly);

        $this->dtgPhone = new ContactPhoneDataGrid($this);
        $this->dtgPhone->SetDataBinder('dtgPhone_Bind');
        $this->dtgPhone->ShowFilter = false;
        $this->dtgPhone->ShowFooter = true;

        $this->dtgPhone->Paginator = $this->dtgPhone->PaginatorAlternate = new QPaginator($this->dtgPhone);
        $this->dtgPhone->ItemsPerPage = 10;

        $this->dtgPhone->AddColumn(new QDataGridColumn('Number', '<?= $_FORM->dtgPhone_NumberColumnRender($_ITEM) ?>', 'HtmlEntities=false', 'Width=90%', array('OrderByClause' => QQ::OrderBy(QQN::ContactPhone()->Number), 'ReverseOrderByClause' => QQ::OrderBy(QQN::ContactPhone()->Number, false))));
        $this->dtgPhone->AddColumn(new QDataGridColumn(null, '<?=$_FORM->dtgPhone_ActionColumnRender($_ITEM)?>', 'HtmlEntities=false'));

        $this->txtNumber = new QTextBox($this->dtgPhone);
        $this->txtNumber->Required = true;
        $this->txtNumber->LabelForRequiredUnnamed = QApplication::Translate('Number is required');
        $this->txtNumber->MaxLength = 20;
        $this->txtNumber->AddAction(new QEnterKeyEvent(), new QAjaxAction('btnSave_Click'));
        $this->txtNumber->AddAction(new QEnterKeyEvent(), new QTerminateAction());

        $this->btnCancel = new QLinkButton($this->dtgPhone);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->SetCustomStyle('margin-left', '10px');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));
        $this->btnCancel->AddAction(new QClickEvent(), new QTerminateAction());
    }

    public function dtgPhone_NumberColumnRender(ContactPhone $objPhone) {

        if (($objPhone->Id == $this->intId) || (($this->intId == -1) && (!$objPhone->Id)))
            return $this->txtNumber->RenderWithError(false);
        else
            return QApplication::HtmlEntities($objPhone->Number);
    }

    public function dtgPhone_ActionColumnRender(ContactPhone $objPhone) {

        if (($objPhone->Id == $this->intId) || (($this->intId == -1) && (!$objPhone->Id))) {

            $strControlId = 'btnSave' . $objPhone->Id;

            $btnSave = $this->GetControl($strControlId);
            if (!$btnSave) {
                $btnSave = new QLinkButton($this->dtgPhone, $strControlId);
                $btnSave->Text = QApplication::Translate('Save');
                $btnSave->CausesValidation = true;
                $btnSave->ActionParameter = $objPhone->Id;
                $btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
                $btnSave->AddAction(new QClickEvent(), new QTerminateAction());
            }

            return $btnSave->Render(false) . $this->btnCancel->Render(false, 'CssClass="muted"');
            
        } else {

            $strControlId = 'btnEdit' . $objPhone->Id;

            $btnEdit = $this->GetControl($strControlId);
            if (!$btnEdit) {
                $btnEdit = new QLinkButton($this->dtgPhone, $strControlId);
                $btnEdit->Text = QApplication::Translate('Edit');
                $btnEdit->ActionParameter = $objPhone->Id;
                $btnEdit->AddAction(new QClickEvent(), new QAjaxAction('btnEdit_Click'));
                $btnEdit->AddAction(new QClickEvent(), new QTerminateAction());
            }

            $btnEdit->Enabled = true;
            if ($this->intId)
                $btnEdit->Enabled = false;
            
            $strControlId = 'btnDelete' . $objPhone->Id;

            $btnDelete = $this->GetControl($strControlId);
            if (!$btnDelete) {
                $btnDelete = new QLinkButton($this->dtgPhone, $strControlId);
                $btnDelete->Text = QApplication::Translate('Delete');
                $btnDelete->SetCustomStyle('margin-left', '10px');
                $btnDelete->ActionParameter = $objPhone->Id;
                $btnDelete->AddAction(new QClickEvent(), new QAjaxAction('btnDelete_Click'));
                $btnDelete->AddAction(new QClickEvent(), new QTerminateAction());
            }

            return $btnEdit->Render(false, 'CssClass="text-info"', 'HtmlEntities="false"') . $btnDelete->Render(false, 'CssClass="text-error"', 'HtmlEntities="false"');
        }
    }

    protected function dtgPhone_Bind() {

        $objPhoneArray =
                $this->dtgPhone->DataSource =
                ContactPhone::LoadArrayByContactId(
                        $this->mctContact->Contact->Id, QQ::Clause(
                                $this->dtgPhone->OrderByClause, $this->dtgPhone->LimitClause
                        ));

        if ($this->intId == -1)
            array_push($objPhoneArray, new ContactPhone());

        $this->dtgPhone->DataSource = $objPhoneArray;
    }

    protected function btnEdit_Click($strFormId, $strControlId, $strParameter) {

        $this->intId = $strParameter;

        $objPhone = ContactPhone::Load($this->intId);
        $this->txtNumber->Text = $objPhone->Number;

        $objFocusControlAction = new QFocusControlAction($this->txtNumber);
        QApplication::ExecuteJavaScript($objFocusControlAction->RenderScript($this->txtNumber));
    }

    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        try {
            if ($this->intId == -1) {
                $objPhone = new ContactPhone();
                $objPhone->ContactId = $this->mctContact->Contact->Id;
                $objPhone->LogInsert = QDateTime::Now();
            } else {
                $this->intId = $strParameter;
                $objPhone = ContactPhone::Load($this->intId);
            }

            $objPhone->Number = trim($this->txtNumber->Text);
            $objPhone->LogUser = $this->objCurrentUser->Username;
            $objPhone->Save();

            $this->intId = null;
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate('Contact Phone Saved Successful !'));
            
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->ctlAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
    }

    protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        $this->intId = null;
    }

    protected function btnNew_Click($strFormId, $strControlId, $strParameter) {

        if ($this->intId)
            return false;

        $this->intId = -1;
        $this->txtNumber->Text = null;

        $objFocusControlAction = new QFocusControlAction($this->txtNumber);
        QApplication::ExecuteJavaScript($objFocusControlAction->RenderScript($this->txtNumber));
    }

    protected function btnDelete_Click($strFormId, $strControlId, $strParameter) {

        $objPhone = ContactPhone::Load($strParameter);

        $this->dlgConfirmation->strTitle = QApplication::Translate('Delete Contact Phone');
        $this->dlgConfirmation->strMessage = QApplication::Translate(sprintf('Do you want to delete "%s"?', $objPhone->Number));
        $this->dlgConfirmation->btnConfirm->ActionParameter = $objPhone->Id;
        $this->dlgConfirmation->Show();
    }

    protected function dlgConfirmation_Confirm($strFormId, $strControlId, $strParameter) {
        try {
            $objPhone = ContactPhone::Load($strParameter);
            $objPhone->Delete();

            $this->dtgPhone->Refresh();
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate(sprintf('Contact Phone "%s" Deleted Successful', $objPhone->Number)));
            $this->dlgConfirmation->Hide();
            
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
    }

}

Phone::Run('Phone', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/contact/phone.tpl.php');
?>